package cn.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.dto.Message;
import cn.dto.Result;
import cn.dto.common;
import cn.pojo.Category;
import cn.service.CategoryService;
import cn.service.impl.CategoryServiceImpl;

/**
 * 处理添加分类请求
 * @author 莫文聪
 *
 */
@WebServlet("/addCategoryServlet")
public class addCategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CategoryService categoryService = new CategoryServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//获取参数
		String postdata = common.getPostData(request.getInputStream(), request.getContentLength(), null);
		JSONObject addCagetoryparam = JSON.parseObject(postdata);
		String categoryname = addCagetoryparam.getString("categroy");
		Category category = new Category();
		category.setCategoryname(categoryname);
		Boolean flag = categoryService.addCategory(category);
		Result result = new Result();
		if(flag == true) {
			Message message = new Message(200,"添加文章分类成功");
			result.setMessage(message);
		}else {
			Message message = new Message(400,"添加文章分类失败");
			result.setMessage(message);
		}
		response.getWriter().write(JSON.toJSONString(result));
	}

}
